<?php
namespace app\common\service\lib;



class Bank
{

    /**
     * Author:Yingbo Sun
     * 阿里云银行卡验证
     * @param $accountNo 银行卡号
     * @param $idCard 身份证号
     * @param $name 姓名
     * @return mixed|\think\response\Json
     */
    public static function verifyBank($accountNo,$idCard,$name,$phone)
    {
        $host = config('setting.bank.host');
        $path = config('setting.bank.path');
        $method = "GET";
        $appcode = config('setting.bank.AppCode');//开通服务后 买家中心-查看AppCode
        $headers = array();
        array_push($headers, "Authorization:APPCODE " . $appcode);
        $querys = "idCard=$idCard&accountNo=$accountNo&mobile=$phone&name=".urlencode("$name");
        $bodys = "";
        $url = $host . $path . "?" . $querys;
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, true);
        if (1 == strpos("$" . $host, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        $out_put = curl_exec($curl);

        $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        list($header, $body) = explode("\r\n\r\n", $out_put, 2);
        if ($httpCode == 200) {
            return json_decode($body,true);
        } else {
            if ($httpCode == 400 && strpos($header, "Invalid Param Location") !== false) {
                $data = ['error_code' => 400,'msg' => '参数错误'];
            } elseif ($httpCode == 400 && strpos($header, "Invalid AppCode") !== false) {
                $data = ['error_code' => 400,'msg' => 'AppCode错误'];
            } elseif ($httpCode == 400 && strpos($header, "Invalid Url") !== false) {
                $data = ['error_code' => 400,'msg' => '请求的 Method、Path 或者环境错误'];
            } elseif ($httpCode == 403 && strpos($header, "Unauthorized") !== false) {
                $data = ['error_code' => 400,'msg' => '服务未被授权（或URL和Path不正确）'];
            } elseif ($httpCode == 403 && strpos($header, "Quota Exhausted") !== false) {
                $data = ['error_code' => 400,'msg' => '套餐包次数用完'];
            } elseif ($httpCode == 500) {
                $data = ['error_code' => 400,'msg' => 'API网关错误'];
            } elseif ($httpCode == 0) {
                $data = ['error_code' => 400,'msg' => 'URL错误'];
            } else {
                print($httpCode);
                $headers = explode("\r\n", $header);
                $headList = array();
                foreach ($headers as $head) {
                    $value = explode(':', $head);
                    $headList[$value[0]] = $value[1];
                }
                print($headList['x-ca-error-message']);
                $data = ['error_code' => 400,'msg' => '参数名错误 或 其他错误'];

            }
        }
        return json($data);


    }
}